from django.shortcuts import render
from django.core.cache import cache
from app01 import models
import ast
from django.conf import settings


def connect(request):
    # print(request.method)
    mid = request.GET.get('id')
    # text = request.GET.get('text')  # 设备名
    device = models.Device.objects.get(id=mid)  # 设备
    toProduct_id = device.toProduct_id
    device_name = device.device_name
    device_name_alias = device.device_name_alias
    device_secret = device.device_secret
    product = device.toProduct  # 产品
    product_key = product.product_key
    product_key_alias = product.product_key_alias
    example = product.toExample  # 实例
    iotInstanceId = example.iotInstanceId
    iotInstanceId_alias = example.iotInstanceId_alias
    clientId = example.clientId
    host_name = example.host_name
    consumerGroupId = example.consumerGroupId
    platform = example.toPlat  # 平台
    name = platform.name
    accessKey = platform.accessKey
    accessSecret = platform.accessSecret
    alias = {"name": name,
             "iotInstanceId_alias": iotInstanceId_alias,
             "product_key_alias": product_key_alias,
             "device_name_alias": device_name_alias,
             "device_name": device_name  # 做为以后队列唯一标识使用
             }

    # 对应的设备的所有功能定义:
    toFun = models.FunDefine.objects.filter(toProductFuc_id=toProduct_id).all()
    # 对应的设备的功能定义运行数据(取最后一条,做为页面显示用)
    toFun_data = models.DeviceData.objects.filter(toDevice_id=mid).last()
    if toFun_data is not None:
        # 转为真正的字典
        toFun_data = ast.literal_eval(toFun_data.items)

    for func in toFun:
        identifier = func.Identifier  # 功能名称
        dataType = func.dataType  # 功能类型代号
        dataDefine = func.dataDefine  # 数据定义
        if dataType == 4 or dataType == 5:  # 枚举类型 和 布尔类型
            dataDefine = dataDefine.split(':')[1]
            dataDefine = dataDefine.split(';')
            dictDefine = {}
            for fine in dataDefine:
                fine1 = fine.split('~')[0]
                fine2 = fine.split('~')[1]
                dictDefine[fine1] = fine2
            func.dataDefine = dictDefine  # 功能数据定义转为字典给前端
            # 初始化功能定义的数据给前端显示用toFun
            if toFun_data is not None:
                # 给每一项赋值
                for key, value in toFun_data.items():
                    if key == identifier:
                        func.value = value.get('value')
            else:
                # func.value = list(func.dataDefine)[0]
                pass
        else:
            if toFun_data is not None:
                # 给每一项赋值
                for key, value in toFun_data.items():
                    if key == identifier:
                        func.value = value.get('value')
            else:
                if ':' in func.dataDefine:
                    func.value = func.dataDefine.split(':')[1].split('~')[0]
                # else:
                #     func.value = func.dataDefine

    dict_iot = {
        'device_name': device_name,
        'device_name_alias': device_name_alias,
        'device_secret': device_secret,
        'product_key': product_key,
        'product_key_alias': product_key_alias,
        'iotInstanceId': iotInstanceId,
        'iotInstanceId_alias': iotInstanceId_alias,
        'clientId': clientId,
        'host_name': host_name,
        'consumerGroupId': consumerGroupId,
        'name': name,
        'accessKey': accessKey,
        'accessSecret': accessSecret,
    }
    # 把dict_iot 写入django-redis,cache是django-redis的字典存储格式
    cache.set('dict_iot', dict_iot, timeout=None)  # timeout是可选的，表示缓存的过期时间

    # 使用websocket连接是用到的url,有本地和上线以后方式,在settings.py里设置,这里取值
    SOCKETIO_SERVER_URL = getattr(settings, 'SOCKETIO_SERVER_URL', 'default_value')
    return render(request, 'iot/connect.html', {'toFun': toFun, 'alias': alias, 'dict_iot': dict_iot, 'SOCKETIO_SERVER_URL': SOCKETIO_SERVER_URL})
